package com.wx.mall.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wx.mall.common.vo.goods.SkuVo;
import com.wx.mall.feign.GoodListFeignClient;
import com.wx.mall.order.dto.ReOrderDto;
import com.wx.mall.order.entity.*;
import com.wx.mall.order.mapper.ReturnOrderMapper;
import com.wx.mall.order.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.order.vo.GoodReturnItemVo;
import com.wx.mall.order.vo.GoodReturnVo;
import com.wx.mall.order.vo.ReturnOrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 退款订单 服务实现类
 * </p>
 *
 * @author j
 * @since 2020-04-22
 */
@Service
public class ReturnOrderServiceImpl extends ServiceImpl<ReturnOrderMapper, ReturnOrder> implements IReturnOrderService {

    @Autowired
    IReturnOrderItemService service;

    /**
     * 根据type==0筛选分页查询
     * @param dto
     * @return
     */
    @Override
    public List<ReturnOrder> finds(ReOrderDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<ReturnOrder> queryWrapper=new QueryWrapper<>();
        if (dto.getSort()==0){
            queryWrapper.orderByAsc("apply_time").eq("type",0).eq("deletes",1);
        };
        if (dto.getSort()==1){
            queryWrapper.orderByAsc("dispose_time").eq("type",0).eq("deletes",1);
        };
        if (dto.getSort()==2){
            queryWrapper.orderByAsc("return_money").eq("type",0).eq("deletes",1);
        };
        List<ReturnOrder> list= this.list(queryWrapper);
        return list;
    }

    /**
     * 根据type==0筛选查询
     * @param dto
     * @return
     */
    @Override
    public List<ReturnOrder> findList(ReOrderDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<ReturnOrder> queryWrapper=new QueryWrapper<>();
        if (dto.getId()!=null){
            queryWrapper.like("id",dto.getId()).eq("type",0).eq("deletes",1);
        }
        if (dto.getLinkman()!=null){
            queryWrapper.like("linkman",dto.getLinkman()).eq("type",0).eq("deletes",1);
        }
        if (dto.getLinkmanMobile()!=null){
            queryWrapper.like("linkman_mobile",dto.getLinkmanMobile()).eq("type",0).eq("deletes",1);
        }
        if (dto.getApplyTime()!=null){
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(dto.getApplyTime());
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date date=calendar.getTime();
            System.out.println(date);
            queryWrapper.le("apply_time",date).eq("type",0).eq("deletes",1);
        }
        if (dto.getStatus()!=null){
            queryWrapper.like("status",dto.getStatus()).eq("type",0).eq("deletes",1);
        }
        if (dto.getDisposeTime()!=null){
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(dto.getDisposeTime());
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date date1=calendar.getTime();
            System.out.println(date1);
            queryWrapper.le("dispose_time",date1).eq("type",0).eq("deletes",1);
        }
        if (dto.getAdminId()!=null){
            queryWrapper.like("admin_id",dto.getAdminId()).eq("type",0).eq("deletes",1);
        }

        if (dto.getSort()==0){
            queryWrapper.orderByAsc("apply_time").eq("type",0).eq("deletes",1);
        };
        if (dto.getSort()==1){
            queryWrapper.orderByAsc("dispose_time").eq("type",0).eq("deletes",1);
        };
        if (dto.getSort()==2){
            queryWrapper.orderByAsc("return_money").eq("type",0).eq("deletes",1);
        };

        List<ReturnOrder> list=this.list(queryWrapper);
        return list;
    }

    /**
     * 根据type==1筛选分页查询
     * @param dto
     * @return
     */
    @Override
    public List<ReturnOrder> findListAll(ReOrderDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<ReturnOrder> queryWrapper=new QueryWrapper<>();
        if (dto.getSort()==0){
            queryWrapper.orderByAsc("apply_time").eq("type",1).eq("deletes",1);
        };
        if (dto.getSort()==1){
            queryWrapper.orderByAsc("dispose_time").eq("type",1).eq("deletes",1);
        };
        if (dto.getSort()==2){
            queryWrapper.orderByAsc("return_money").eq("type",1).eq("deletes",1);
        };
        return this.list(queryWrapper);
    }

    /**
     * 根据type==1筛选查询
     * @param dto
     * @return
     */
    @Override
    public List<ReturnOrder> findListMs(ReOrderDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<ReturnOrder> queryWrapper=new QueryWrapper<>();
        if (dto.getId()!=null){
            queryWrapper.like("id",dto.getId()).eq("type",1).eq("deletes",1);
        }
        if (dto.getLinkman()!=null){
            queryWrapper.like("linkman",dto.getLinkman()).eq("type",1).eq("deletes",1);
        }
        if (dto.getLinkmanMobile()!=null){
            queryWrapper.like("linkman_mobile",dto.getLinkmanMobile()).eq("type",1).eq("deletes",1);
        }
        if (dto.getApplyTime()!=null){
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(dto.getApplyTime());
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date date=calendar.getTime();
            System.out.println(date);
            queryWrapper.le("apply_time",date).eq("type",1).eq("deletes",1);
        }
        if (dto.getStatus()!=null){
            queryWrapper.like("status",dto.getStatus()).eq("type",1).eq("deletes",1);
        }
        if (dto.getDisposeTime()!=null){
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(dto.getDisposeTime());
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date date1=calendar.getTime();
            System.out.println(date1);
            queryWrapper.le("dispose_time",date1).eq("type",1).eq("deletes",1);
        }
        if (dto.getAdminId()!=null){
            queryWrapper.like("admin_id",dto.getAdminId()).eq("type",1).eq("deletes",1);
        }
        if (dto.getSort()==0){
            queryWrapper.orderByAsc("apply_time").eq("type",1).eq("deletes",1);;
        };
        if (dto.getSort()==1){
            queryWrapper.orderByAsc("dispose_time").eq("type",1).eq("deletes",1);;
        };
        if (dto.getSort()==2){
            queryWrapper.orderByAsc("return_money").eq("type",1).eq("deletes",1);;
        };
        List<ReturnOrder> list=this.list(queryWrapper);
        return list;
    }

    /**
     * 根据改变type查询
     * @param status
     * @return
     */
    @Override
    public int getAllObj(Integer status) {
        QueryWrapper<ReturnOrder> wrapper=new QueryWrapper<>();
        if (status!=null){
            wrapper.eq("status",status).eq("type",0).eq("deletes",1);
        }
        int count=this.count(wrapper);
        return count;
    }

    /**
     * 根据改变type查询
     * @param status
     * @return
     */
    @Override
    public int getAllObjTwo(Integer status) {
        QueryWrapper<ReturnOrder> wrapper=new QueryWrapper<>();
        if (status!=null){
            wrapper.eq("status",status).eq("type",1).eq("deletes",1);
        }
        int count=this.count(wrapper);
        return count;
    }

    /**
     * 根据是否已拒绝来删除
     * @param vo
     * @return
     */
    @Override
    public int deletes(GoodReturnVo vo) {
        List<ReturnOrder> list=new ArrayList<>();
        for (Long id : vo.getIds()){
            ReturnOrder returnOrder=this.getById(id);
            if (returnOrder.getStatus()==3){
                returnOrder.setDeletes(0);
                list.add(returnOrder);
            }
        }
        boolean returnOrders=this.updateBatchById(list);
        return returnOrders?1:0;
    }

    /**
     * 根据id修改状态
     * @param vo
     * @return
     */
    @Override
    public int updateStatus(GoodReturnVo vo) {
        List<ReturnOrder> list=new ArrayList<>();
        for (Long id:vo.getIds()
             ) {
            ReturnOrder order=this.getById(id);
            if (order.getStatus()!=2){
                order.setStatus(2);
            }
            list.add(order);
        }
        boolean success=this.updateBatchById(list);
        return success?1:0;
    }

    /**
     * 根据id秀修改状态2
     * @param vo
     * @return
     */
    @Override
    public int updateStatus2(GoodReturnVo vo) {
        List<ReturnOrder> list1=new ArrayList<>();
        for (Long id:vo.getIds()
        ) {
            ReturnOrder order=this.getById(id);
            if (order.getStatus()!=3){
                order.setStatus(3);
            }
            list1.add(order);
        }
        boolean success=this.updateBatchById(list1);
        return success?1:0;
    }

}
